/**
 * Created by apple on 16/3/17.
 */
import './style.scss'
class Controller {
    constructor (systemService, statusMap, uiNotification, item, $modalInstance) {
        "ngInject"
        this.notify    = uiNotification;
        this.uiModal   = $modalInstance;
        this.service   = systemService;
        this.select    = { status: statusMap };
        this.item      = item;
        this.data      = item ? angular.copy(item) : {statusCd: "S0A"};
        this.type      = item ? 'update' : 'insert';
        this.isEdit    = item ? true : false;
    }

    checkValid (field) {
        if ( field.$pristine ) {
            field.$pristine = false;
        }
    }

    save () {
        if ( this.submitting ) return;
        this.submitting = true;
        if ( this.item ) {
            delete this.data.commonRegion;
            delete this.data.platInfo;
        }
        this.service[ this.type ]('plat', this.data)
            .then(() => {
                this.uiModal.close(this.data);
                this.notify.success({
                    'title'  : '提示',
                    'message': '操作成功!'
                });
            }).catch((data) => {
                this.notify.error({
                    'title'  : '提示',
                    'message': '操作失败. ' + (data.resultMsg ? data.resultMsg : '')
                });
            }).finally(() => {
                // 隐藏 loading
                this.submitting = false;
            });
    }
    cancel () {
        this.uiModal.dismiss('cancel');
    }
}

export default Controller;